<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<button type="button" id="addStu">新增</button>
<table>
    <tr>
        <th>学号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>出身日期</th>
        <th>操作</th>
    </tr>


</table>
总记录数：<span id="countNo"></span>总页数：<span id="countPageNo"></span>
<div id="pageChange">
</div>
<input type="hidden" id="pageNo" name="pageNo" value="1">

<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
    $(function () {
        var countPageNo = 0;

        function initStudent() {
            console.log("in ...");
            var pageNo = $("#pageNo").val();
            console.log("pageNo：" + pageNo);
            $.getJSON("studentServlet", "pageNo=" + pageNo + "&opr=pageInfo", successInit);

            function successInit(data) {
                console.log("data:" + data);
                // 维护改变当前页码值
                $("#pageNo").val(data.currentPageNo);
                // 清除学生对象
                $("tr:gt(0)").remove();
                // 解析展示学生对象
                for (var i = 0; i < data.studentList.length; i++) {
                    var student = data.studentList[i];
                    var $tr = $("<tr></tr>");
                    $tr.append("<td>" + student.studentNo + "</td>")
                        .append("<td>" + student.studentName + "</td>")
                        .append("<td>" + student.sex + "</td>")
                        .append("<td>" + student.bornDate + "</td>")
                        .append("<td><a href='#' class='delStu' stuNo='" + student.studentNo + "'>删除</a><a href='update.html?studentNo=" + student.studentNo + "'>修改</a></td>");
                    $("table>tbody").append($tr);
                }
                //    总页数
                countPageNo = data.countPageNo;
                $("#countPageNo").html(countPageNo);
                //    总记录数
                $("#countNo").html(data.countNo);

                // if ()
                $("#pageChange>a").remove();


                $("#pageChange").append("<a href='#' id='firstPage' >首页</a>")
                    .append("<a href='#' id='prePage' >上一页</a>").append("<a href='#' id='nextPage' >下一页</a>")
                    .append("<a href='#' id='lastPage' >末页</a>");


            }
        }

        // 调用initStudent函数
        initStudent();

        $(document).on("click", "#nextPage,#prePage,#firstPage,#lastPage", function () {
            var pageClick = $(this).attr("id");
            console.log("pagecclick:" + pageClick);
            switch (pageClick) {
                case "nextPage":
                    var currentPageNo = $("#pageNo").val();
                    var pageNo = parseInt(currentPageNo) + 1;
                    if (pageNo > countPageNo) {
                        return;
                    }
                    $("#pageNo").val(pageNo);
                    break;
                case "prePage":
                    var currentPageNo = $("#pageNo").val();
                    var pageNo = parseInt(currentPageNo) - 1;
                    if (pageNo < 1) {
                        return;
                    }
                    $("#pageNo").val(pageNo);
                    break;
                case "firstPage":
                    $("#pageNo").val(1);
                    break;
                case "lastPage":
                    $("#pageNo").val(countPageNo);
                    break;
                default:
                    $("#pageNo").val(1);
                    break;
            }
            initStudent();
        });

        $(document).on("click", ".delStu", function () {
            var stuNo = $(this).attr("stuNo");
            console.log("studentNo:" + stuNo);
            $.post("studentServlet", "opr=del&studentNo=" + stuNo, successDel, "JSON");

            function successDel(data) {
                if (data == true) {
                    alert("删除成功！！");
                    window.location.reload();
                } else {
                    alert("删除失败");
                }
            }
        });

        $("#addStu").click(function () {
            window.location.href = "http://localhost:8080/addStu.html";
        });

    });


</script>
</body>
</html>